﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Mst;
using ESOL.Business.Prod;
using ESOL.Common;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManagePickingPlan : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars
        private ProdPickingMaterialEntity mSelectedProdPickingMaterial;
        private List<ProdPickingMaterialEntity> listSelectedPickingMaterial;
        #endregion

        #region ctors
        public frmManagePickingPlan()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void SetButtom()
        {
            if (mSelectedProdPickingMaterial != null)
            {
                switch (mSelectedProdPickingMaterial.Status)
                {
                    case "PIK01":
                        btnModify.Enabled = true;
                        btnDelete.Enabled = true;
                        break;
                    case "PIK02":
                        btnModify.Enabled = false;
                        btnDelete.Enabled = false;
                        break;
                    case "PIK05":
                        btnModify.Enabled = false;
                        btnDelete.Enabled = false;
                        break;
                }
            }
        }

        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPickType, "PIKT", true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PIK", true);
            DataTable dtPrintStatus = new DataTable();
            dtPrintStatus.Columns.Add("name");
            dtPrintStatus.Columns.Add("value", typeof(int));
            dtPrintStatus.Rows.Add("", null);
            dtPrintStatus.Rows.Add("是", 1);
            dtPrintStatus.Rows.Add("否", 0);
            cmbPrintStatus.DataSource = dtPrintStatus;
            cmbPrintStatus.DisplayMember = "name";
            cmbPrintStatus.ValueMember = "value";

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "PIKT", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit2, "PIK", false);
        }

        private void SetField()
        {
            cmbStatus.SetSelectedValue("PIK02");
            cmbPrintStatus.SetSelectedValue(0);
        }

        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtPick.Text) == false)
            {
                condition += " AND PPM.PICKING_CODE LIKE '%" + txtPick.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbPickType.SelectedValue.ToString()) == false && cmbPickType.SelectedValue != null)
            {
                condition += " AND PPM.PICKING_TYPE  = '" + cmbPickType.SelectedValue.ToString() + "'";
            }
            if (dtpCreateDate.EditValue!=null)
            {
                condition += " AND PPM.CREATED_DATE>='" + ((DateTime)dtpCreateDate.EditValue).ToString("yyyy-MM-dd") + " 00:00:00' ";
            }
            if (dtpCreateDateTo.EditValue != null)
            {
                condition += " AND PPM.CREATED_DATE<='" + ((DateTime)dtpCreateDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                condition += " AND PPM.STATUS='" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            if (string.IsNullOrEmpty(cmbPrintStatus.SelectedValue.ToString()) == false)
            {
                condition += " AND PPM.PRINT_STATUS='" + cmbPrintStatus.SelectedValue.ToString() + "' ";
            }
            return condition;
        }

        private void BindGridMain()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = " PPM.CREATED_DATE DESC";
            DataTable dt = ProdPickingMaterialBs.GetByCondition(condition, orderBy, rowCount);

            DataColumn dtCol = new DataColumn();
            dtCol.ColumnName = "IS_SELECTED";
            dtCol.DataType = System.Type.GetType("System.Boolean");
            dtCol.DefaultValue = false;
            dt.Columns.Add(dtCol);
            gvMain.DataSource = dt;
        }

        private void BindGridDetail(string PickingID)
        {
            if (string.IsNullOrEmpty(PickingID))
            {
                gvDetail.DataSource = null;
                return;
            }
            string Condition = " AND PPMI.PICKING_MATERIAL_ID='" + PickingID + "' ";
            DataTable dt = ProdPickingMaterialItemBs.GetDtPickingMaterialItemByCondition(Condition, "");
            if (dt == null || dt.Rows.Count == 0)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = dt;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            SetField();
            BindGridMain();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle < 0)
                return;
            DataTable dt = (DataTable)gvMain.DataSource;
            if ((bool)dt.Rows[e.RowHandle]["IS_SELECTED"])
            {
                dt.Rows[e.RowHandle]["IS_SELECTED"] = false;
            }
            else
            {
                dt.Rows[e.RowHandle]["IS_SELECTED"] = true;
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle < 0)
                return;
            mSelectedProdPickingMaterial = new ProdPickingMaterialEntity(gridView1.GetDataRow(e.FocusedRowHandle));
            BindGridDetail(mSelectedProdPickingMaterial.PickingMaterialId);
            SetButtom();
        }

        protected override void Click_View()
        {
            base.Click_View();
            if (mSelectedProdPickingMaterial == null)
            {
                MessageBox.Show("请选择一张下料单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPickingPlan frm = new frmMaintainPickingPlan(mSelectedProdPickingMaterial);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainPickingPlan frm = new frmMaintainPickingPlan();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindGridMain();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedProdPickingMaterial == null)
            {
                MessageBox.Show("请选择一个下料单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPickingPlan frm = new frmMaintainPickingPlan(mSelectedProdPickingMaterial);
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindGridMain();
            }
        }

        protected override void Click_Delete()
        {
            if (mSelectedProdPickingMaterial == null)
            {
                MessageBox.Show("请选择一张下料单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedProdPickingMaterial.Status.Equals("PIK02"))
            {
                MessageBox.Show("下料单状态已完成，无法删除！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中下料单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedProdPickingMaterial.Status = PickOrder.PIK05.ToString();
                mSelectedProdPickingMaterial.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedProdPickingMaterial.LastModifiedDate = DateTime.Now;
                bool successful = ProdPickingMaterialBs.UpdateProdPickingMaterial(mSelectedProdPickingMaterial);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindGridMain();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnPickingOrderPrint_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gvMain.DataSource;
            listSelectedPickingMaterial = new List<ProdPickingMaterialEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                if ((bool)dr["IS_SELECTED"] == true)
                {
                    ProdPickingMaterialEntity m = new ProdPickingMaterialEntity(dr);
                    listSelectedPickingMaterial.Add(m);
                }
            }
            if (listSelectedPickingMaterial.Count == 0)
            {
                MessageBox.Show("请选择下料单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            StringBuilder Conditon = new StringBuilder(" AND PPMI.PICKING_MATERIAL_ID IN (");
            foreach (ProdPickingMaterialEntity mProdPickingMaterial in listSelectedPickingMaterial)
            {
                Conditon.Append("'" + mProdPickingMaterial.PickingMaterialId + "',");
            }
            Conditon.Replace(',', ')', Conditon.Length - 1, 1);
            DataTable dt1 = ProdPickingMaterialItemBs.GetDtPickingMaterialItemByCondition(Conditon.ToString(), "");
            frmPrintFormPickingPlanOrder frm = new frmPrintFormPickingPlanOrder(listSelectedPickingMaterial[0], dt1);
            frm.ShowDialog();
        }

        private void btnForgePrint_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gvMain.DataSource;
            listSelectedPickingMaterial = new List<ProdPickingMaterialEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                if ((bool)dr["IS_SELECTED"] == true)
                {
                    ProdPickingMaterialEntity m = new ProdPickingMaterialEntity(dr);
                    listSelectedPickingMaterial.Add(m);
                }
            }
            if (listSelectedPickingMaterial.Count == 0)
            {
                MessageBox.Show("请选择下料单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            StringBuilder Conditon = new StringBuilder(" AND PPMI.PICKING_MATERIAL_ID IN (");
            foreach (ProdPickingMaterialEntity mProdPickingMaterial in listSelectedPickingMaterial)
            {
                Conditon.Append("'" + mProdPickingMaterial.PickingMaterialId + "',");
            }
            Conditon.Replace(',', ')', Conditon.Length - 1, 1);
            DataTable dt1 = ProdPickingMaterialItemBs.GetDtPickingMaterialItemByCondition(Conditon.ToString(), "");
            //frmPrintFormForgeOrder frm = new frmPrintFormForgeOrder(dt1);
            //frm.ShowDialog();
        }

        protected override void Click_Query()
        {
            BindGridMain();
        }

        #endregion
    }
}
